﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Exhibition.Common.Helper;
using Exhibition.Dao.ViewModel;
using Exhibition.Service;

namespace Exhibition
{
    public partial class Reg2 :BasePage
    {
        UserModel umd = new UserModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "用户详细信息填写"+WebTitle;
            if (stmd != null) {
                if (stmd.IsAsk == 0)
                {
                    imgAsk.Visible = true;
                    imgAsk.Src = "images/reglv.gif";
                    spask.Visible = true;
                    spask.Text = "调查问卷";
                }
                else
                {
                    imgAsk.Visible = false;
                    spask.Visible = false;

                }
            }

            if (Session["RegUser"] != null)
            {
             
                umd = Session["RegUser"] as UserModel;
            }
            if (umd != null) {
                ltrUName.Text = umd.UserName;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = false;

            var verifycode=System.Guid.NewGuid().ToString().Replace("-","");
            //用户基础数据
            umd.RealName = Request["ture_name"];
            umd.Sex = Convert.ToInt32(Request["sex"]);
            umd.CreateTime = DateTime.Now;
            umd.IsDelete = 0;
            umd.Properties = Request["ywxz"];
            umd.Depart = Depart.Value;
            umd.Position = Position.Value;
            umd.Mobile = Request["Mobile"];
            umd.verifycode = verifycode;
            umd.CompName = Request["CompName"];
            umd.IsCheck = 0;
            umd.UserAddress = Request["CompAddr"];
            //公司基础数据
            UserCompModel compmdl = new UserCompModel();
            compmdl.SiteID = stmd.Id;
            //compmdl.UserId = umd.Id; 
            compmdl.CompName = Request["CompName"];
            compmdl.Compnature = Request["ywxz"];
            compmdl.Lincense = Request["Lincense"];
            compmdl.RegYear = string.IsNullOrEmpty(RegYear.Value) ? DateTime.Now.Year : Convert.ToInt32(RegYear.Value);
            compmdl.RegMoney = string.IsNullOrEmpty(RegMoney.Value) ? 0 : Convert.ToInt32(RegMoney.Value);
            compmdl.UserNum = string.IsNullOrEmpty(UserNum.Value) ? 0 : Convert.ToInt32(UserNum.Value);
            compmdl.CompAddr = Request["CompAddr"];
            compmdl.CountryCode = CountryCode.Value;
            compmdl.PhoneStart = PhoneStart.Value;
            compmdl.PhoneNum = PhoneNum.Value;
            compmdl.FaxStart = FaxStart.Value;
            compmdl.FaxNum = FaxNum.Value;
            compmdl.CompSite = CompSite.Value;
            compmdl.CompLevel = 1;
            compmdl.IsDelete = 0;
            compmdl.CreateTime = DateTime.Now;

            var uid = CenterService.userService.Save(umd);
            if (uid > 0) {
                UserLogModel ulog = new UserLogModel();
                compmdl.UserId = uid;
                ulog.UserId = uid;
                ulog.Content = "用户：" + umd.UserName + "与" + DateTime.Now + "注册成为系统的" + (umd.UserType == 1 ? "观众" : "展商");
                ulog.CreateTime = DateTime.Now;
                CenterService.userLogService.Save(ulog);
                CenterService.userCompService.Save(compmdl);
            }
            if (!string.IsNullOrEmpty(stmd.EmailReg))
            {
                var uri = "http://" + Request.Url.Host + (Request.Url.Port == 80 ? "" : ":" + Request.Url.Port) + "/VerifyUser.aspx?verifycode=" + verifycode;


                var url = "<a href='" + uri + "' target='_blank'>" + uri + "</a>";

                var body = stmd.EmailReg.Replace("{User}", umd.UserName)
                                        .Replace("{SiteName}", stmd.WebName)
                                        .Replace("{Date}", DateTime.Now.ToString("yyyy-MM-dd"))
                                        .Replace("{Url}", url);


                EmailHelper ehelper = new EmailHelper(stmd.WebEmail, stmd.EmailPwd, stmd.WebEmail, umd.UserName, stmd.WebName + "网站注册", body, stmd.StmpServer, true);
                ehelper.Send();
            }

            if (stmd.IsAsk == 0)
            {
                //进入调查问卷系统

            }
            else {
                Response.Redirect("RegSuccess.aspx");
            }
        }
    }
}